package com.jelly.web.modules.message.service.impl;

import com.jelly.common.constant.Constant;
import com.jelly.common.constant.MessageConstant;
import com.jelly.common.util.StringUtils;
import com.jelly.config.shiro.token.TokenUtil;
import com.jelly.web.modules.message.dao.MessageMapper;
import com.jelly.web.modules.message.domain.Message;
import com.jelly.web.modules.message.domain.MessageExample;
import com.jelly.web.modules.message.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @创建人 ql
 * @创建时间 2018-10-31 09:54
 * @项目名称 jelly
 * @功能描述: 消息接口实现
 **/
@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private MessageMapper messageMapper;

    /**
     * 新增消息
     * @return
     */
    @Override
    public boolean addMessage(Message message) {

        message.setMsgId(UUID.randomUUID().toString().replaceAll("-", ""));
        message.setAddDate(new Date());
        message.setIsRead(MessageConstant.READNO);
        return messageMapper.insertSelective(message) > 0;

    }

    /**
     * 通过id获取消息内容
     * @param msgId
     * @return
     */
    @Override
    public Message getMessageId(String msgId) {
        return messageMapper.selectByPrimaryKey(msgId);
    }

    /**
     * 获取当前用户的全部消息
     * @return
     */
    @Override
    public List<Message> getMessageAll(Message message) {

        MessageExample example = new MessageExample();
        MessageExample .Criteria criteria = example.createCriteria().andTargetIdEqualTo(TokenUtil.getUserId());

        if(StringUtils.isNotBlank(message.getIsRead())){
            criteria.andIsReadEqualTo(message.getIsRead());
        }

        if(StringUtils.isNotBlank(message.getMsgType())){
            criteria.andMsgTypeEqualTo(message.getMsgType());
        }

        if(StringUtils.isNotBlank(message.getTargetType())){
            criteria.andTargetTypeEqualTo(message.getTargetType());
        }

        example.setOrderByClause("add_date desc");

        return messageMapper.selectByExampleWithBLOBs(example);

    }

    /**
     * 获取当前用户的未读消息
     * @param read
     * @return
     */
    @Override
    public List<Message> getMessageAllRead(String read) {

        return messageMapper.selectByExampleWithBLOBs(getExample(read));

    }

    /**
     * 获取当前用户的未读消息数量
     * @param read
     * @return
     */
    @Override
    public Integer getMessageReadCount(String read) {

        return messageMapper.countByExample(getExample(read));

    }


    public MessageExample getExample(String read){
        MessageExample example = new MessageExample();
        example.createCriteria().andTargetIdEqualTo(TokenUtil.getUserId()).andIsReadEqualTo(read);
        example.setOrderByClause("add_date desc");
        return example;
    }


    /**
     * 批量修改消息已读状态
     * @param mesgIds
     * @return
     */
    @Override
    public boolean updateMessageListRead(List<String> mesgIds) {

        MessageExample example = new MessageExample();
        example.createCriteria().andTargetIdEqualTo(TokenUtil.getUserId()).andMsgIdIn(mesgIds);
        Message message = new Message();
        message.setIsRead(MessageConstant.READYES);
        message.setIsReadDate(new Date());

        return messageMapper.updateByExample(message,example) > 0;

    }

    /**
     * 修改消息已读状态
     * @param mesgId
     * @return
     */
    @Override
    public boolean updateMessageRead(String mesgId) {

        Message message = new Message();
        message.setMsgId(mesgId);
        message.setIsRead(MessageConstant.READYES);
        message.setIsReadDate(new Date());

        return messageMapper.updateByPrimaryKey(message) > 0;

    }

}
